<template>
  <div id="userManagePage">
    <a-form layout="inline" :model="searchParams" @finish="doSearch">
      <a-form-item label="账号">
        <a-input v-model:value="searchParams.userAccount" placeholder="输入账号" />
      </a-form-item>
      <a-form-item label="用户名">
        <a-input v-model:value="searchParams.userName" placeholder="输入用户名" />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit">搜索</a-button>
      </a-form-item>
      <a-form-item>
        <a-button type="default" @click="resetSearch">重置</a-button>
      </a-form-item>
    </a-form>
    <br />
    <a-table
      :columns="columns"
      :data-source="dataList"
      :pagination="pagination"
      @change="doTableChange"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'userAvatar'">
          <a-image :src="record.userAvatar" alt="头像" style="width: 50px; height: 50px" />
        </template>
        <template v-if="column.key === 'userProfile'">
          <a-tooltip :title="record.userProfile">
            <div class="ellipsis">{{ record.userProfile }}</div>
          </a-tooltip>
        </template>
        <template v-if="column.key === 'userRole'">
          <a-tag :color="record.userRole === 'admin' ? 'blue' : 'green'">
            {{ record.userRole === 'admin' ? '管理员' : '普通用户' }}
          </a-tag>
        </template>
        <template v-if="column.key === 'createTime'">
          {{ formatDateTime(record.createTime) }}
        </template>
        <template v-if="column.key === 'action'">
          <a-button danger @click="handleDelete(record)">删除</a-button>
        </template>
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { computed, onMounted, reactive, ref } from 'vue'
import { deleteUserUsingPost, listUserVoByPageUsingPost } from '@/api/userController'
import { message } from 'ant-design-vue'
import { Tooltip as ATooltip } from 'ant-design-vue'
import dayjs from 'dayjs'
import { Modal } from 'ant-design-vue'

const columns = [
  { title: 'ID', dataIndex: 'id', key: 'id' },
  { title: '账号', dataIndex: 'userAccount', key: 'userAccount' },
  { title: '用户名', dataIndex: 'userName', key: 'userName' },
  { title: '头像', dataIndex: 'userAvatar', key: 'userAvatar' },
  { title: '简介', dataIndex: 'userProfile', key: 'userProfile' },
  { title: '角色', dataIndex: 'userRole', key: 'userRole' },
  { title: '创建时间', dataIndex: 'createTime', key: 'createTime' },
  { title: '操作', key: 'action', dataIndex: 'action' },
]

const dataList = ref([])
const total = ref(0)

const searchParams = reactive<API.UserQueryRequest>({
  pageNum: 1,
  pageSize: 10,
})

const fetchData = async () => {
  const res = await listUserVoByPageUsingPost({
    ...searchParams,
  })
  if (res.data.data) {
    dataList.value = res.data.data.records ?? []
    total.value = res.data.data.total ?? 0
  } else {
    message.error('获取用户列表失败' + res.data.message)
  }
}

// 格式化日期时间
const formatDateTime = (dateTime: string | number | Date) => {
  return dayjs(dateTime).format('YYYY-MM-DD HH:mm:ss')
}

const handleDelete = (record: any) => {
  Modal.confirm({
    title: '确认删除',
    content: `确定要删除用户 ${record.userName} 吗？`,
    okText: '删除',
    okType: 'danger',
    cancelText: '取消',
    async onOk() {
      try {
        const res = await deleteUserUsingPost({
          id: record.id,
        })
        if (res.data.code === 20000) {
          message.success('删除成功')
          await fetchData()
        } else {
          message.error('删除失败' + res.data.message)
        }
      } catch (error) {
        message.error('删除失败，请稍后再试')
      }
    },
    onCancel() {
      console.log('取消删除')
    },
  })
}

// 分页参数
const pagination = computed(() => {
  return {
    current: searchParams.pageNum ?? 1,
    pageSize: searchParams.pageSize ?? 10,
    total: total.value,
    showSizeChanger: true,
    showTotal: (total: number) => `共 ${total} 条`,
  }
})

// 表格变化处理
const doTableChange = (page: any) => {
  searchParams.pageNum = page.current
  searchParams.pageSize = page.pageSize
  fetchData()
}

// 获取数据
const doSearch = () => {
  // 重置页码
  searchParams.pageNum = 1
  fetchData()
}
// 重置搜索
const resetSearch = () => {
  searchParams.userAccount = ''
  searchParams.userName = ''
  searchParams.pageNum = 1 // 重置页码
  fetchData()
}

onMounted(() => {
  fetchData()
})
</script>

<style scoped>
.ellipsis {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 300px; /* 根据需要调整宽度 */
}
</style>
